<?php

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
$date_start = date('Y-m-d',strtotime("-1 day")) .' 00:00:00';
$date_start1 = date('Ymd',strtotime("-1 day"));
//$date_start = '2017-07-05 00:00:00';
$date_end = date('Y-m-d',strtotime("-1 day")) .' 23:59:59';
$date_end1 = date('Ymd',strtotime("-1 day"));
//$date_end = '2017-07-05 23:59:59';
print_r("……………………………… " . date('Y-m-d H:i:s') . " start running get_pos_more_info.php………………………………");
////写入 随行付 的记录
//FinanceStatement::putSXPRecords($date_start1,$date_end1);
$mdb = DB::select("select * from maintenance_pos_device WHERE status != '2'");
foreach ($mdb as $_mdb) {
    $totalPage = 1;
    $pageIndex = 1;
    $size = 30;
    $access_token = Pos::getFinanceToken();
    Log::debug(array('device_no'=>$_mdb->device_no));
    Log::debug(array('access_token'=>$access_token));
    while($pageIndex <= $totalPage) {
        try {
            $result = Pos::prepareFinance($_mdb->device_no, $date_start, $date_end, $access_token, $pageIndex, $size);
        }catch(Exception $e){
            print_r($e);
            $pageIndex += 1;
            continue;
        }
        if ($result && isset($result->data)) {
            print_r("get_pos_more_info.php 返回结果：" . $result->data);
            $result1 = json_decode($result->data);
            if (isset($result1->data) && isset($result1->data->list)) {
                if(sizeof($result1->data->list) == 0){
                    $pageIndex += 1;
                    continue;
                }
                $page = $result1->page;
                $totalPage = $page->totalPage;
                $pageIndex += 1;
                $resu = $result1->data->list;
                print_r("get_pos_more_info.php 返回结果数组大小：" . sizeof($result1->data->list));
                $rate_rec = WposFinanceRate::where('status',1)->orderBy('id','desc')->first();
                foreach ($resu as $res) {
                   // if (!isset($res->thirdChannelSerialno) || $res->thirdChannelSerialno == '') {
                   //     continue;
                   // }
                    try {
                        $wfd = WposFinanceDetail::where('id', $res->id);
                        if (isset($res->mcode)) {
                            $wfd = $wfd->where('mcode', $res->mcode);
                        }
                        if (isset($res->orderno)) {
                            $wfd = $wfd->where('orderno', $res->orderno);
                        }
                        if (isset($res->thirdChannelSerialno)) {
                            $wfd = $wfd->where('thirdChannelSerialno', $res->thirdChannelSerialno);
                        }
                        $wfd = $wfd->first();
                        if (!$wfd) {
                            $wfd = new WposFinanceDetail();
                        }
                        $wfd->id = isset($res->id) ? $res->id : '';
                        $wfd->mcode = isset($res->mcode) ? $res->mcode : '';
                        $wfd->orderno = isset($res->orderno) ? $res->orderno : '';
                        $wfd->amount = isset($res->amount) ? $res->amount : 0;
                        $wfd->payCash = isset($res->payCash) ? $res->payCash : 0;
                        $wfd->discount = isset($res->discount) ? $res->discount : 0;
                        $wfd->thirdDiscount = isset($res->thirdDiscount) ? $res->thirdDiscount : 0;
                        $wfd->refundAmount = isset($res->refundAmount) ? $res->refundAmount : 0;
                        $wfd->deduction = isset($res->deduction) ? $res->deduction : 0;
                        $wfd->balanceAmount = isset($res->balanceAmount) ? $res->balanceAmount : 0;
                        $wfd->discountAmount = isset($res->discountAmount) ? $res->discountAmount : 0;
                        $wfd->memberRulesAmount = isset($res->memberRulesAmount) ? $res->memberRulesAmount : 0;
                        $wfd->fee = isset($res->fee) ? $res->fee : 0;
                        $wfd->subsidy = isset($res->subsidy) ? $res->subsidy : 0;
                        $wfd->inAmt = isset($res->inAmt) ? $res->inAmt : 0;
                        $wfd->recvAmt = isset($res->recvAmt) ? $res->recvAmt : 0;
                        $wfd->incomeAmt = isset($res->incomeAmt) ? $res->incomeAmt : 0;
                        $wfd->status = isset($res->status) ? $res->status : 0;
                        $wfd->statusText = isset($res->statusText) ? $res->statusText : '';
                        $wfd->payModeId = isset($res->payModeId) ? $res->payModeId : 0;
                        $wfd->payModeName = isset($res->payModeName) ? $res->payModeName : '';
                        $wfd->payChannelId = isset($res->payChannelId) ? $res->payChannelId : 0;
                        $wfd->payChannelName = isset($res->payChannelName) ? $res->payChannelName : '';
                        $wfd->thirdSerialno = isset($res->thirdSerialno) ? $res->thirdSerialno : '';
                        $wfd->payDate = isset($res->payDate) ? $res->payDate : '';
                        $wfd->deviceEn = isset($res->deviceEn) ? $res->deviceEn : '';
                        $wfd->operator = isset($res->operator) ? $res->operator : '';
                        $wfd->isOutOrder = isset($res->isOutOrder) ? $res->isOutOrder : 0;
                        $wfd->thirdChannelSerialno = isset($res->thirdChannelSerialno) ? $res->thirdChannelSerialno : '';
                        $wfd->note = isset($res->note) ? $res->note : '';
                        $wfd->batchno = isset($res->batchNo) ? $res->batchNo : '';
                        $wfd->vouchno = isset($res->vouchNo) ? $res->vouchNo : '';
                        $wfd->refno = isset($res->refNo) ? $res->refNo : '';
                        $wfd->province = isset($res->province) ? $res->province : '';
                        $wfd->city = isset($res->city) ? $res->city : '';
                        $wfd->cardIssuresNo = isset($res->cardIssuresNo) ? $res->cardIssuresNo : '';
                        $wfd->cardOrg = isset($res->cardOrg) ? $res->cardOrg : '';
                        $wfd->terminalNo = isset($res->terminalNo) ? $res->terminalNo : '';
                        $wfd->payDay = isset($res->payDay) ? $res->payDay : '';
                        $wfd->payMonth = isset($res->payMonth) ? $res->payMonth : '';
                        $wfd->cardType = isset($res->cardType) ? $res->cardType : 0;
                        $wfd->feeType = isset($res->feeType) ? $res->feeType : '';
                        $wfd->mname = isset($res->mname) ? $res->mname : '';
                        $wfd->createDate = isset($res->createDate) ? $res->createDate : '';
                        //计算手续费
                        if($rate_rec){
                            $an_fee = 0;
                            $wfd->rate_id = $rate_rec->id;
                            if($wfd->status == '2') {
                                if ($wfd->payModeId == '1006') {
                                    if ($wfd->cardType == 1 && $rate_rec->in_debit_rate) {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->in_debit_rate,$rate_rec->in_debit_sin_top);
                                    } elseif ($wfd->cardType == 2 && $rate_rec->in_credit_rate) {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->in_credit_rate,$rate_rec->in_credit_sin_top);
                                    } elseif ($wfd->cardType == 0 && $rate_rec->out_rate) {// 境外卡
                                        $an_fee = getAmount($wfd->amount,$rate_rec->out_rate,$rate_rec->out_sin_top);
                                    }
                                } elseif ($wfd->payModeId == '1003') {
                                    if ($rate_rec->b2c_wechat_rate) {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->b2c_wechat_rate,$rate_rec->b2c_wechat_sin_top);
                                    }
                                } elseif ($wfd->payModeId == '1004') {
                                    if ($rate_rec->b2c_alipay_rate) {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->b2c_alipay_rate,$rate_rec->b2c_alipay_sin_top);
                                    }
                                }
                            }elseif($wfd->status == '3'){
                                if ($wfd->payModeId == '1006') {
                                    if ($wfd->cardType == 1 && $rate_rec->in_debit_rate && $rate_rec->in_debit_back == '1') {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->in_debit_rate,$rate_rec->in_debit_sin_top);
                                    } elseif ($wfd->cardType == 2 && $rate_rec->in_credit_rate && $rate_rec->in_credit_back == '1') {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->in_credit_rate,$rate_rec->in_credit_sin_top);
                                    } elseif ($wfd->cardType == 0 && $rate_rec->out_rate && $rate_rec->out_back == '1') {// 境外卡
                                        $an_fee = getAmount($wfd->amount,$rate_rec->out_rate,$rate_rec->out_sin_top);
                                    }
                                } elseif ($wfd->payModeId == '1003') {
                                    if ($rate_rec->b2c_wechat_rate && $rate_rec->b2c_wechat_back == '1') {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->b2c_wechat_rate,$rate_rec->b2c_wechat_sin_top);
                                    }
                                } elseif ($wfd->payModeId == '1004') {
                                    if ($rate_rec->b2c_alipay_rate && $rate_rec->b2c_alipay_back == '1') {
                                        $an_fee = getAmount($wfd->amount,$rate_rec->b2c_alipay_rate,$rate_rec->b2c_alipay_sin_top);
                                    }
                                }
                            }
                            $wfd->anhui_fee = $an_fee;
                            $wfd->anhui_income_amt = $wfd->amount - $wfd->refundAmount - $an_fee;
                        }
                        $wfd->save();
                        // 对账
                        FinanceStatement::statement(2,$res->id);
                    } catch (Exception $e) {
                        print_r($e);
                        Log::error(json_encode($e->getMessage()));
                    }
                }
            }
        }
    }
}
function getAmount($amount,$rate,$top){
    $temp5 = round(($amount / 100) * ($rate / 100000), 2) * 100;
    if ($top) {
        $an_fee = $temp5 > $top ? $top : $temp5;
    } else {
        $an_fee = $temp5;
    }
    return $an_fee;
}
print_r("……………………………… " . date('Y-m-d H:i:s') . " stop running get_pos_more_info.php………………………………");
?>